package com.hospital.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.util.DbUtil;

@WebServlet("/manage/InhospitalCureServlet")
public class InhospitalCureServlet extends HttpServlet {
	//住院期间的病情开药管理
	protected void doPost(HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			String orderid = request.getParameter("orderid").trim();
			String account = request.getParameter("account").trim();
			String cases = request.getParameter("cases").trim();
			DbUtil dbutil = new DbUtil();
			String ypmc = request.getParameter("ypmc").trim();
			String ypsl = request.getParameter("ypsl").trim();
			String item = (String)request.getParameter("item").trim();
			int j = 0;
			j = dbutil.getIf(ypmc);
			int k = 0;
			k = dbutil.getSale(ypmc);
			if(Integer.parseInt(ypsl) > (j-k)){//首先要进行药品是否足够的检查
				request.setAttribute("message", "该药品共进货"+j+",累计出售"+k+",剩余"+(j-k)+",您需要出售的数量为"+ypsl+"，操作失败！");
				request.getRequestDispatcher("/manage/Inhospitable_cure_add.jsp").forward(request, response);
			}else{
				String ypsalemoney = null;
				String sqlmoeny = "select * from t_medicinename where id = "+ ypmc;
				List<Map<String, Object>> listmoney = DbUtil.find(sqlmoeny);
				Map<String, Object> mapmoeny = null;
				String yapname = null;
				//从药品的名字表中得到药品的销售价格
				for (int i = 0; i < listmoney.size(); i++) {
					mapmoeny = (Map<String, Object>)listmoney.get(i);
					ypsalemoney = (String)mapmoeny.get("money");
					yapname = (String)mapmoeny.get("mname");
				}
				double ypsj = Double.parseDouble(ypsalemoney);
				String sumjg = String.valueOf(Integer.parseInt(ypsl) * ypsj);
				String sqlitemmoney = "select * from t_checklist where id = "+ item;
				List<Map<String, Object>> listitemmoney = DbUtil.find(sqlitemmoney);
				Map<String, Object> mapitemmoney = null;
				String itemmoney = null;
				for (int i = 0; i < listmoney.size(); i++) {
					mapitemmoney = (Map<String, Object>)listitemmoney.get(i);
					itemmoney = (String)mapitemmoney.get("price");
				}
				String sql = "insert into t_inhospital_record(orderid, account,cases, drug, drugmoney, item, itemmoney) values('" 
						+ orderid + "', '" + account + "', '" + cases + "','" + yapname + "'," + ypsj + ",'" + item + "'," + itemmoney + ")";
				DbUtil.add_modify(sql);
				//将医生开的药进行处理
				String sqlyp = "insert into t_sale(ypmc,ypsj,ypsl,sumjg,xsrq) values('"
						+ ypmc +"', '" + ypsj + "', '" + ypsl + "','" + sumjg + "', '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlyp);
				String sqlsy = "update t_stroage set ypsysl = '" + (j-k-Integer.parseInt(ypsl)) + "' where ypmc = " + ypmc;
				DbUtil.add_modify(sqlsy);
				String sqlcheckmoney = "insert into t_check_money(checkname, money, addtime) values('"
						+ item + "', '" + itemmoney + "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlcheckmoney);
				//将医生开的检查单进行处理
				double totalprices = ypsj + Double.parseDouble(itemmoney);
				String sql_money = "update t_patient_info set money = money - '" + totalprices + "' where account = '" + account + "'";
				DbUtil.add_modify(sql_money);
				request.getRequestDispatcher("/manage/Inhospitable_cure_manage.jsp").forward(request, response);
			}
		} else if ("del".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			DbUtil.del("t_inhospital_record", id);
			request.getRequestDispatcher("/manage/Inhospitable_cure_manage.jsp").forward(request, response);
		}
	}
}